﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Security.Policy;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;
using static System.Net.Mime.MediaTypeNames;
using static CoreShop.Library.GlobalEnumVars;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodServices : Repository<corecmsfood>, IcorecmsfoodServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfood entity)
        {
            return base.Change<corecmsfood>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfood> entity)
        {
            return base.InsertOrUpdate(entity);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfood entity)
        {
            return base.Change<corecmsfood>().Update(a => new corecmsfood()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                foodTypeId = entity.foodTypeId,
                foodSpecId = entity.foodSpecId,
                type = entity.type,
                number = entity.number,
                name = entity.name,
                byName = entity.byName,
                businessType = entity.businessType,
                sellStatus = entity.sellStatus,
                specFlag = entity.specFlag,
                isHeft = entity.isHeft,
                helpCode = entity.helpCode,
                pinYin = entity.pinYin,
                warrantyValue = entity.warrantyValue,
                warrantyType = entity.warrantyType,
                isPrint = entity.isPrint,
                isChangePrice = entity.isChangePrice,
                isDiscount = entity.isDiscount,
                //minDiscount = entity.minDiscount,
                upSell = entity.upSell,
                addSell = entity.addSell,
                image = entity.image,
                remark = entity.remark,
                pointType = entity.pointType,
                pointValue = entity.pointValue,
                modifyTime = entity.modifyTime
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfood> entity)
        {
            //List<corecmsfoodcategory> items = childrens.Where(p => p.parentId == item.id).ToList();
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfood>().Update(a => new corecmsfood()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    foodTypeId = item.foodTypeId,
                    foodSpecId = item.foodSpecId,
                    type = item.type,
                    number = item.number,
                    name = item.name,
                    byName = item.byName,
                    businessType = item.businessType,
                    sellStatus = item.sellStatus,
                    specFlag = item.specFlag,
                    isHeft = item.isHeft,
                    helpCode = item.helpCode,
                    pinYin = item.pinYin,
                    warrantyValue = item.warrantyValue,
                    warrantyType = item.warrantyType,
                    isPrint = item.isPrint,
                    isChangePrice = item.isChangePrice,
                    isDiscount = item.isDiscount,
                    //minDiscount = item.minDiscount,
                    upSell = item.upSell,
                    addSell = item.addSell,
                    image = item.image,
                    remark = item.remark,
                    pointType = item.pointType,
                    pointValue = item.pointValue,
                    modifyTime = item.modifyTime
                }, a => a.id == item.id);
            }
            return returnKey;

        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdatePriceAsync(List<corecmsfood> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodspec>().Update(a => new corecmsfoodspec()
                {
                    posPrice = item.posPrice,
                }, a => a.id == item.foodSpecIds);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfood>().Update(a => new corecmsfood()
            {
                isDelete = true,
                //updateTime = DateTime.Now
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfood>().Update(a => new corecmsfood()
                {
                    isDelete = true,
                    //updateTime = DateTime.Now
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfood> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        /// <summary>
        /// 根据商品类型ID查询类型名称
        /// </summary>
        /// <param name="custoType"></param>
        /// <returns></returns>
        public corecmsfood QueryById(long id)
        {
            corecmsfood food = base.Change<corecmsfood>().GetSingle(a => a.id == id);
            if (food != null)
            {
                corecmsfoodspec spec = base.Change<corecmsfoodspec>().GetSingle(a => a.id == food.foodSpecId);
                if (spec != null)
                {
                    food.foodUnitName = spec.specUnitName;
                    food.foodSpecName = spec.specName;
                }
            }
            return food;
        }

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfood> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfood> lst = new List<corecmsfood>();
            var where = Expressionable.Create<corecmsfood>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.name.Contains(sName) || a.number.Contains(sName) || a.helpCode.Contains(sName) || a.pinYin.Contains(sName) || a.byName.Contains(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.number).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.number).ToList();
            }

            return lst;
        }
                

        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfood> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, Expressionable<corecmsfood, corecmsfoodspec> where)
        {
            List<corecmsfood> lst = new List<corecmsfood>();            
            lst = base.AsQueryable().LeftJoin<corecmsfoodspec>((f, s) => f.foodSpecId == s.id)                    
                    .WhereIF(true, where.ToExpression())
                    .Select((f, s) => new corecmsfood
                    {
                        id = f.id,
                        number = s.number,
                        name = f.name,
                        byName = f.byName,
                        helpCode = f.helpCode,
                        pinYin = f.pinYin,
                        isChangePrice = f.isChangePrice,
                        isDiscount = f.isDiscount,
                        image = f.image,
                        foodSpecName = s.specName,
                        foodUnitName = s.specUnitName,
                        posPrice = s.posPrice,
                        isHeft = f.isHeft,
                        code = s.code,
                        foodId = s.foodId,
                        foodSpecIds = s.id,
                        foodSpecId = s.id,
                        vipPrice = s.vipPrice,
                        purPrice = s.purPrice,
                        rmoPrice = s.rmoPrice,
                        type = f.type,
                        stand = f.stand,
                        weighPlu = s.weighPlu,
                        isSelected = 1,
                    }).OrderBy(f => f.helpCode)
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .ToList();            

            if (lst.Count > 0)
            {
                //获取调价单
                var spuIds = lst.Select(f => f.foodSpecId).ToList();
                var listPrice = new corecmschgpriceServices().GetChgPriceList(spuIds);
                if (listPrice != null &&  listPrice.Count > 0)
                {
                    foreach (var item in lst)
                    {
                        var findItem = listPrice.Find(p => p.pdtSpuSpecId == item.foodSpecId);
                        if (findItem != null) item.posPrice = findItem.newSalePrice;
                    }
                }
            }

            return lst;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfood> QueryAsync(long typeId, string foodName)
        {
            List<corecmsfood> list = new List<corecmsfood>();
            var where = Expressionable.Create<corecmsfood>();
            where = where.And(a => a.isDelete == false && a.id > 0);
            if (typeId > 0)
            {
                where = where.And(a => a.foodTypeId == typeId);
            }
            if (foodName != null || foodName != "")
            {
                where = where.And(a => a.name.Contains(foodName));
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.id).ToList();
            return list;
        }
        #endregion

        #region
        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfood> QueryByIdsAsync(List<long> Ids)
        {
            List<corecmsfood> list = new List<corecmsfood>();
            var where = Expressionable.Create<corecmsfood>();
            if (Ids.Count > 0)
            {
                where = where.And(a => Ids.Contains(a.id));
            }
            list = base.AsQueryable().WhereIF(true, where.ToExpression()).ToList();
            return list;
        }
        #endregion
    }
}
